<?php

namespace app\logic\model\music;

use app\logic\model\LogicModel;
use app\logic\model\user\UserLogicModel;
use think\model\concern\SoftDelete;

class MusicLogicModel extends LogicModel
{
    use SoftDelete;

    protected $name='music';

    protected $deleteTime = 'delete_time';

    function Writer(){
        return $this->hasOne(UserLogicModel::class,'id','user_id');
    }
    function type(){
        return $this->hasOne(MusicTypeLogicModel::class,'id','type_id');
    }

    public static function getIdsByTitle($title)
    {
        return self::whereLike('title',"%{$title}%")->column('id');
    }

    public static function getInfoById($id,$field='*')
    {
        return self::where('id',$id)->field($field)->find();
    }

    public static function getInfosByIds($id=[],$field='*')
    {
        return self::whereIn('id',$id)->field($field)
            ->with(['Writer'])
            ->visible(['Writer'=>['writer_name','writer_head_img','fans_num']])
            ->select()->toArray();
    }

    public static function getListByIds($ids=[],$field='*')
    {
        return self::whereIn('id',$ids)->column($field,'id');
    }
    public static function getIdsByUserId($userId)
    {
        return self::whereIn('user_id',$userId)->column('id');
    }
    public static function getUserIdByMusicId($music_id)
    {
        if(is_string($music_id)){
            return self::where('id',$music_id)->value('user_id');
        }else{
            return self::whereIn('id',$music_id)->column('user_id');
        }

    }
    function getList($request,$where=[],$filed="*",$page=1,$limit=15,$order="id desc"){
        $query=$this->where($where)
            ->where(function ($q) use ($request){
                if(check_request($request,'title')){
                    $q->whereLike("title","%{$request['title']}%");
                }
                if(check_request($request,'user_id')){
                    $q->where("user_id",$request['user_id']);
                }
                if(check_request($request,'id')){
                    $q->where("id",$request['id']);
                }
                if(check_request($request,'type_id')){
                    $q->where("type_id",$request['type_id']);
                }
                if(check_request($request,'start_time')&&check_request($request,'end_time')){
                    $q->whereBetween("create_time",[$request['start_time'],$request['end_time']." 23:59:59"]);
                }elseif (check_request($request,'start_time')){
                    $q->where("create_time",">=",$request['start_time']);
                }elseif (check_request($request,'end_time')){
                    $q->where("create_time","<=",$request['end_time']." 23:59:59");
                }
            });
        $list = $query->field($filed)->order($order)
            ->with(['type'])
            ->page($page,$limit)->select();
        $count = $query->count();
        return [$list,$count];
    }
}